/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aliasi.annotate.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author Diogo
 */
public class SelectingComboSample {

    public static void main(String args[]) {
		String labels[] = { "A", "B", "D" };
		JFrame frame = new JFrame("Selecting JComboBox");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Mudar a frame por forma a permitir a utilização dos cursores para mudar o "focus"
		Set forwardKeys = new HashSet();
	    forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0));
	    forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0));
		Set backwardKeys = new HashSet();
	    backwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));
	    frame.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,forwardKeys);
	    frame.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,backwardKeys);

	    // Permitir a utilização de teclas para seleccionar elementos da ComboBox
	    JComboBox.KeySelectionManager manager = new JComboBox.KeySelectionManager() {
			public int selectionForKey(char aKey, ComboBoxModel aModel) {
				switch ( aKey ) {
					case 'a' : case 'A' : return 0;
					case 'b' : case 'B' : return 1;
					case 'c' : case 'C' : return 2;
				}
				return -1;
			}
		};
		JComboBox comboBox1 = new JComboBox(labels);
		frame.add(comboBox1, BorderLayout.SOUTH);
		comboBox1.setKeySelectionManager(manager);

		JComboBox comboBox2 = new JComboBox(labels);
		frame.add(comboBox2, BorderLayout.NORTH);
		comboBox2.setKeySelectionManager(manager);

		frame.setSize(400, 200);
		frame.setVisible(true);
	}

}
